<template>
  <div>
    <!--留言簿-->
    <content-header :title="routeItem.title"></content-header>
    <div class="form xui-padding-top-lg">
      <el-form :model="form" :rules="rules" ref="form" label-width="100px" class="demo-form">
        <el-form-item label="您的姓名：" prop="name">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="手机号码：" prop="mobile">
          <el-input v-model="form.mobile"></el-input>
        </el-form-item>
        <el-form-item label="留言内容：" prop="content">
          <el-input type="textarea" v-model="form.content"></el-input>
        </el-form-item>
        <el-form-item label="邮箱地址：" prop="email">
          <el-input v-model="form.email"></el-input>
        </el-form-item>
        <el-form-item style="text-align: center">
          <el-button type="primary" @click="submitForm('form')">提交</el-button>
          <el-button @click="resetForm('form')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import ContentHeader from "../../../components/widge/content-header";
import { saveMessageFromUser } from "@/api/interact";

export default {
  components: { ContentHeader },
  data() {
    return {
      form: {
        name: "",
        email: "",
        mobile: "",
        content: ""
      },
      rules: {
        name: [
          { required: true, message: "请输入您的名字", trigger: "blur" },
          { min: 2, message: "长度在 3 到 5 个字符", trigger: "blur" }
        ],
        email: [{ type: "email", message: "请输入邮箱", trigger: "blur" }],
        mobile: [
          { required: true, message: "情输入您的电话", trigger: "blur" },
          {
            pattern: /^1[3|4|5|6|7|8][0-9]\d{4,8}$/,
            message: "请输入正确的电话",
            trigger: "blur"
          }
        ],
        content: [
          { required: true, message: "请填写留言内容", trigger: "blur" },
          {
            min: 10,
            max: 500,
            message: "留言内容在10～500字之间",
            trigger: "blur"
          }
        ]
      }
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          saveMessageFromUser(this.form).then(res => {
            if (res.flag == 1) {
              this.$message({
                showClose: true,
                type: "success",
                message: "提交成功"
              });
              this.$refs.form.resetFields();
            } else {
              this.$message({
                showClose: true,
                type: "error",
                message: res.msg
              });
              this.$refs.form.resetFields();
            }
          });
        } else {
          console.log("error submit!!");
          this.$message({
            showClose: true,
            type: "error",
            message: "输入信息有误，请确认后重试"
          });
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  },
  computed: {
    routeItem() {
      let routes = this.$store.getters.siteMap;
      let result;
      routes.forEach(route => {
        if (route.children) {
          route.children.forEach(child => {
            if (child.name == this.$route.name) {
              result = child;
            }
          });
        }
      });
      return result;
    }
  }
};
</script>

<style lang='less' scoped>
.form {
  width: 496px;
  margin: 0 auto;
}
.el-button {
  display: inline-block;
  line-height: 1;
  white-space: nowrap;
  cursor: pointer;
  background: #dbad23;
  border: 1px solid #dbad23;
  color: #fff;
  -webkit-appearance: none;
  text-align: center;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
  outline: 0;
  /*margin: 0;*/
  -webkit-transition: 0.1s;
  transition: 0.1s;
  padding: 12px 20px;
  font-size: 14px;
  border-radius: 2px;
  width: 118px;
  height: 38px;
  margin-right: 30px;
}
.el-upload--picture-card {
  background-color: #fbfdff;
  border: 1px dashed #c0ccda;
  border-radius: 6px;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
  width: 96px;
  height: 96px;
  line-height: 96px;
  vertical-align: top;
}
</style>
